perf: add ASCII fast path in parseGround#287
Open
dyxushuai wants to merge 2 commits intorockorager:mainfrom
Open
perf: add ASCII fast path in parseGround#287dyxushuai wants to merge 2 commits intorockorager:mainfrom
dyxushuai wants to merge 2 commits intorockorager:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a performance optimization for parsing ASCII keypress events by introducing a fast path that bypasses UTF-8 decoding and grapheme iteration for printable ASCII characters (0x20-0x7E). The fast path only applies when the next byte is also ASCII or absent, ensuring multi-codepoint graphemes like combining marks and keycap sequences that start with ASCII are correctly handled by the slower path.
Key changes:
- Added fast path in
parseGroundfor printable ASCII with safety check for multi-byte sequences - Added benchmark comparison showing 3.35x speedup for ASCII input
- Added uucode dependency to benchmark imports for baseline measurement
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Parser.zig | Adds fast path for ASCII characters (0x20-0x7E) that skips UTF-8/grapheme processing when next byte is ASCII or absent |
| bench/bench.zig | Adds baseline benchmark function and comparison tests for ASCII parsing performance |
| build.zig | Adds uucode module import to benchmark executable for baseline comparison |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parseGround handles the common ASCII keypress path by running UTF-8 decoding and grapheme iteration even for single-byte printable ASCII. This adds avoidable overhead for each keystroke.
Fix
Add a fast path for printable ASCII (0x20..0x7E) that returns a keypress directly. We only take the fast path when the next byte is ASCII (or absent) to avoid multi-codepoint graphemes like combining marks/keycap sequences that start with ASCII but continue with non-ASCII bytes. Control bytes and ESC keep the existing mapping rules.
Bench (local, zig build bench, iterations=200, 80x24)
Baseline = pre-fast-path work (UTF-8 + grapheme iteration)
Fast = current parseGround fast path
Improvement: ASCII 'a' -73.7% (3.80x); Mixed stream -36.6% (1.58x).
Tests